package com.client.data;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.shared.model.Assure;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class AssureListGrid extends ListGrid {

	public AssureListGrid() {
		ListGridField civilField = new ListGridField("civilite", " ");
		civilField.setWidth(20);
		ListGridField nomField = new ListGridField("nom", "Nom");
		nomField.setWidth(100);
		ListGridField prenomField = new ListGridField("prenom", "Prenom");
		prenomField.setWidth(100);
		ListGridField dateNaissanceField = new ListGridField("dateNaissance", "Date de naissance");
		dateNaissanceField.setWidth((new String("Date de naissance")).length() * 6);
		ListGridField datePermisField = new ListGridField("datePermis", "Date d'obtention du permis");
		datePermisField.setWidth((new String("Date d'obtention du permis")).length() * 6);
		ListGridField permisField = new ListGridField("permis", "Permis suspendu");
		permisField.setWidth(100);
		ListGridField adresseField = new ListGridField("adresse", "Adresse");
		adresseField.setWidth(200);
		ListGridField mailField = new ListGridField("mail", "E-Mail");
		mailField.setWidth(150);
		ListGridField numeroTelField = new ListGridField("numeroTel", "Numero de telephone");
		numeroTelField.setWidth((new String("Numero de telephone")).length() * 7);
		ListGridField numeroMobileField = new ListGridField("numeroMobile", "Numero de mobile");
		numeroMobileField.setWidth((new String("Numero de mobile")).length() * 7);
		
		ListGridField[] fields = new ListGridField[]{civilField, nomField, prenomField, dateNaissanceField, datePermisField,
			permisField, adresseField, mailField, numeroTelField, numeroMobileField};
		
		setFields(fields);
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setHeight("300px");
		setAutoFitData(Autofit.HORIZONTAL);
		setAutoFitMaxWidth(670);		
		setCellHeight(40);
		setWrapCells(true);
	}
	
	public void addData(Assure assure){
		ListGridRecord record = new ListGridRecord();	
		
		record.setAttribute("civilite", assure.getCivilite());		
		record.setAttribute("nom", assure.getNom());		
		record.setAttribute("prenom", assure.getPrenom());		
		record.setAttribute("dateNaissance",assure.getDateNaissanceToString());		
		record.setAttribute("datePermis",assure.getDatePermisToString());		
		if (assure.isPermisAnnule())
		{			
			record.setAttribute("permis","Oui, jusque : " + assure.getDateReobtentionToString());
		}
		else
		{
			record.setAttribute("permis","Non");
		}		
		record.setAttribute("adresse",assure.getAdresse1().getAdresse());		
		record.setAttribute("mail", assure.getMail());		
		record.setAttribute("numeroTel", assure.getNumeroTel());		
		record.setAttribute("numeroMobile", assure.getNumeroMobile());
		addData(record);
	}
	
	
	
	public List<Integer> getSelection(ListGridRecord[] records){
		List<Integer> result = new ArrayList<Integer>();
		for(Record r : records) result.add(getRecordIndex(r));
		return result;		
	}
}
